package org.example.cet.demos.controller;

import jakarta.validation.Valid;
import org.example.cet.demos.converter.converter;
import org.example.cet.demos.domain.PageBean;
import org.example.cet.demos.domain.Result;
import org.example.cet.demos.domain.form.ClassroomForm;
import org.example.cet.demos.domain.po.Classroom;
import org.example.cet.demos.domain.query.ClassroomQuery;
import org.example.cet.demos.service.impl.ClassroomServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hq
 * @since 2024-12-18
 */
@RestController
@RequestMapping("/classroom")
public class ClassroomController {
    @Autowired
    private ClassroomServiceImpl classroomService;
    @Autowired
    private converter converter;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody ClassroomForm classroomForm) {
        Classroom classroom=converter.ToClassroom(classroomForm);
        classroomService.save(classroom);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable String id) {
        classroomService.removeById(id);
        return Result.success();
    }

    @PutMapping("/update/{id}")
    public Result update(@PathVariable String id,@Valid @RequestBody ClassroomForm classroomForm) {
        Classroom classroom=converter.ToClassroom(classroomForm);
        classroom.setId(id);
        classroomService.updateById(classroom);
        return Result.success();
    }

    @GetMapping("/getPage")
    public Result getPage(ClassroomQuery classroomQuery) {
        PageBean pb=classroomService.getPage(classroomQuery);
        return Result.success(pb);
    }

    @GetMapping("/findStu/{id}")
    public Result findStu(@PathVariable String id) {
        return Result.success(classroomService.findStu(id));
    }

    @GetMapping("/getList")
    public Result getList() {
        return Result.success(classroomService.getList());
    }
}
